package ThreadTest;

import java.util.concurrent.locks.ReentrantLock;

public class TicketTest3 {
    public static void main(String[] args) {
        TicketSell3 ticketSell3 = new TicketSell3();
        Thread thread1 = new Thread(ticketSell3);
        Thread thread2 = new Thread(ticketSell3);
        Thread thread3 = new Thread(ticketSell3);
        thread1.setName("窗口1");
        thread2.setName("窗口2");
        thread3.setName("窗口3");
        thread1.start();
        thread2.start();
        thread3.start();
    }
}

class TicketSell3 implements Runnable{
    private static int tickets = 100;
    public static int getTickets() {
        return tickets;
    }
    ReentrantLock reentrantLock = new ReentrantLock();
    @Override
    public void run() {
        while (true){
            try {
                reentrantLock.lock();
                if(tickets > 0){
                    System.out.println(Thread.currentThread().getName()+":\t"+tickets + "\t->\t" +(tickets-1));
                    tickets--;
                }
            }finally {
                reentrantLock.unlock();
            }

        }
    }
}